import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;

class server{
    int port;
    ServerSocket serversocket;
    Socket clientsocket;
    BufferedReader in;
    ObjectOutputStream out;
    
    public server(int port){
        this.port=port;
        
        try{
            System.out.println("creating server");
            serversocket=new ServerSocket(port);
            System.out.println("server created");
            System.out.println("awaiting connection");
            clientsocket=serversocket.accept();
            System.out.println("connection accepted");
            in=new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
            out=new ObjectOutputStream(clientsocket.getOutputStream());
        }
        catch(java.io.IOException e){
            System.out.println(e);
            System.out.println("couldnt create server on port:"+port);
        }
        
        try{
            SendByte(ReadFile("serverfile.txt"));
        }
        catch(IOException e){
            System.out.println(e);
            System.out.println("error couldnt find or send file");
        }
    }
    public void SendByte(byte[] bytearray)throws java.io.IOException{
        out.writeObject(bytearray);
    }
    public String GetString()throws java.io.IOException{        
        return in.readLine();
    }
    public byte[] ReadFile(String filename) throws IOException { //Only for smaller files?
        InputStream in = new FileInputStream(filename);
        byte[] data = new byte[in.available()];
        in.read(data);
        return data;
    }
    public static void main(String arg[]){
        new server(8899);
    }
}
